<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - include/caffe/solver.hpp</title>
  <link rel="stylesheet" type="text/css" href="../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../index.html">top level</a> - <a href="index.html">include/caffe</a> - solver.hpp<span style="font-size: 80%;"> (source / <a href="solver.hpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">1</td>
            <td class="headerCovTableEntry">12</td>
            <td class="headerCovTableEntryLo">8.3 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:50:33</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">1</td>
            <td class="headerCovTableEntry">20</td>
            <td class="headerCovTableEntryLo">5.0 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #ifndef CAFFE_SOLVER_HPP_</a>
<span class="lineNum">       2 </span>            : #define CAFFE_SOLVER_HPP_
<span class="lineNum">       3 </span>            : #include &lt;boost/function.hpp&gt;
<span class="lineNum">       4 </span>            : #include &lt;string&gt;
<span class="lineNum">       5 </span>            : #include &lt;vector&gt;
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : #include &quot;caffe/net.hpp&quot;
<span class="lineNum">       8 </span>            : #include &quot;caffe/solver_factory.hpp&quot;
<span class="lineNum">       9 </span>            : #include &quot;caffe/util/benchmark.hpp&quot;
<span class="lineNum">      10 </span>            : 
<span class="lineNum">      11 </span>            : namespace caffe {
<span class="lineNum">      12 </span>            : 
<span class="lineNum">      13 </span>            : /**
<span class="lineNum">      14 </span>            :   * @brief Enumeration of actions that a client of the Solver may request by
<span class="lineNum">      15 </span>            :   * implementing the Solver's action request function, which a
<span class="lineNum">      16 </span>            :   * client may optionally provide in order to request early termination
<span class="lineNum">      17 </span>            :   * or saving a snapshot without exiting. In the executable caffe, this
<span class="lineNum">      18 </span>            :   * mechanism is used to allow the snapshot to be saved when stopping
<span class="lineNum">      19 </span>            :   * execution with a SIGINT (Ctrl-C).
<span class="lineNum">      20 </span>            :   */
<span class="lineNum">      21 </span>            :   namespace SolverAction {
<span class="lineNum">      22 </span>            :     enum Enum {
<span class="lineNum">      23 </span>            :       NONE = 0,  // Take no special action.
<span class="lineNum">      24 </span>            :       STOP = 1,  // Stop training. snapshot_after_train controls whether a
<span class="lineNum">      25 </span>            :                  // snapshot is created.
<span class="lineNum">      26 </span>            :       SNAPSHOT = 2  // Take a snapshot, and keep training.
<span class="lineNum">      27 </span>            :     };
<span class="lineNum">      28 </span>            :   }
<span class="lineNum">      29 </span>            : 
<span class="lineNum">      30 </span>            : /**
<span class="lineNum">      31 </span>            :  * @brief Type of a function that returns a Solver Action enumeration.
<span class="lineNum">      32 </span>            :  */
<span class="lineNum">      33 </span>            : typedef boost::function&lt;SolverAction::Enum()&gt; ActionCallback;
<span class="lineNum">      34 </span>            : 
<span class="lineNum">      35 </span>            : /**
<span class="lineNum">      36 </span>            :  * @brief An interface for classes that perform optimization on Net%s.
<span class="lineNum">      37 </span>            :  *
<span class="lineNum">      38 </span>            :  * Requires implementation of ApplyUpdate to compute a parameter update
<span class="lineNum">      39 </span>            :  * given the current state of the Net parameters.
<span class="lineNum">      40 </span>            :  */
<span class="lineNum">      41 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      42 </span>            : class Solver {
<span class="lineNum">      43 </span>            :  public:
<span class="lineNum">      44 </span>            :   explicit Solver(const SolverParameter&amp; param);
<span class="lineNum">      45 </span>            :   explicit Solver(const string&amp; param_file);
<span class="lineNum">      46 </span>            :   void Init(const SolverParameter&amp; param);
<span class="lineNum">      47 </span>            :   void InitTrainNet();
<span class="lineNum">      48 </span>            :   void InitTestNets();
<span class="lineNum">      49 </span>            : 
<span class="lineNum">      50 </span>            :   // Client of the Solver optionally may call this in order to set the function
<span class="lineNum">      51 </span>            :   // that the solver uses to see what action it should take (e.g. snapshot or
<span class="lineNum">      52 </span>            :   // exit training early).
<span class="lineNum">      53 </span>            :   void SetActionFunction(ActionCallback func);
<span class="lineNum">      54 </span>            :   SolverAction::Enum GetRequestedAction();
<span class="lineNum">      55 </span>            :   // The main entry of the solver function. In default, iter will be zero. Pass
<a name="56"><span class="lineNum">      56 </span>            :   // in a non-zero iter number to resume training for a pre-trained net.</a>
<span class="lineNum">      57 </span>            :   virtual void Solve(const char* resume_file = NULL);
<span class="lineNum">      58 </span><span class="lineNoCov">          0 :   inline void Solve(const string&amp; resume_file) { Solve(resume_file.c_str()); }</span>
<span class="lineNum">      59 </span>            :   void Step(int iters);
<span class="lineNum">      60 </span>            :   // The Restore method simply dispatches to one of the
<span class="lineNum">      61 </span>            :   // RestoreSolverStateFrom___ protected methods. You should implement these
<span class="lineNum">      62 </span>            :   // methods to restore the state from the appropriate snapshot type.
<span class="lineNum">      63 </span>            :   void Restore(const char* resume_file);
<span class="lineNum">      64 </span>            :   // The Solver::Snapshot function implements the basic snapshotting utility
<span class="lineNum">      65 </span>            :   // that stores the learned net. You should implement the SnapshotSolverState()
<span class="lineNum">      66 </span>            :   // function that produces a SolverState protocol buffer that needs to be
<a name="67"><span class="lineNum">      67 </span>            :   // written to disk together with the learned net.</a>
<a name="68"><span class="lineNum">      68 </span>            :   void Snapshot();</a>
<a name="69"><span class="lineNum">      69 </span><span class="lineCov">          3 :   virtual ~Solver() {}</span></a>
<a name="70"><span class="lineNum">      70 </span><span class="lineNoCov">          0 :   inline const SolverParameter&amp; param() const { return param_; }</span></a>
<span class="lineNum">      71 </span><span class="lineNoCov">          0 :   inline shared_ptr&lt;Net&lt;Dtype&gt; &gt; net() { return net_; }</span>
<span class="lineNum">      72 </span><span class="lineNoCov">          0 :   inline const vector&lt;shared_ptr&lt;Net&lt;Dtype&gt; &gt; &gt;&amp; test_nets() {</span>
<a name="73"><span class="lineNum">      73 </span><span class="lineNoCov">          0 :     return test_nets_;</span></a>
<span class="lineNum">      74 </span>            :   }
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :   int iter() const { return iter_; }</span>
<span class="lineNum">      76 </span>            : 
<span class="lineNum">      77 </span>            :   // Invoked at specific points during an iteration
<span class="lineNum">      78 </span>            :   class Callback {
<span class="lineNum">      79 </span>            :    protected:
<span class="lineNum">      80 </span>            :     virtual void on_start() = 0;
<span class="lineNum">      81 </span>            :     virtual void on_gradients_ready() = 0;
<span class="lineNum">      82 </span>            : 
<span class="lineNum">      83 </span>            :     template &lt;typename T&gt;
<a name="84"><span class="lineNum">      84 </span>            :     friend class Solver;</a>
<a name="85"><span class="lineNum">      85 </span>            :   };</a>
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :   const vector&lt;Callback*&gt;&amp; callbacks() const { return callbacks_; }</span>
<span class="lineNum">      87 </span><span class="lineNoCov">          0 :   void add_callback(Callback* value) {</span>
<span class="lineNum">      88 </span><span class="lineNoCov">          0 :     callbacks_.push_back(value);</span>
<span class="lineNum">      89 </span><span class="lineNoCov">          0 :   }</span>
<span class="lineNum">      90 </span>            : 
<span class="lineNum">      91 </span>            :   void CheckSnapshotWritePermissions();
<span class="lineNum">      92 </span>            :   /**
<a name="93"><span class="lineNum">      93 </span>            :    * @brief Returns the solver type.</a>
<span class="lineNum">      94 </span>            :    */
<span class="lineNum">      95 </span><span class="lineNoCov">          0 :   virtual inline const char* type() const { return &quot;&quot;; }</span>
<span class="lineNum">      96 </span>            : 
<span class="lineNum">      97 </span>            :   // Make and apply the update value for the current iteration.
<span class="lineNum">      98 </span>            :   virtual void ApplyUpdate() = 0;
<span class="lineNum">      99 </span>            : 
<span class="lineNum">     100 </span>            :  protected:
<span class="lineNum">     101 </span>            :   string SnapshotFilename(const string&amp; extension);
<span class="lineNum">     102 </span>            :   string SnapshotToBinaryProto();
<span class="lineNum">     103 </span>            :   string SnapshotToHDF5();
<span class="lineNum">     104 </span>            :   // The test routine
<span class="lineNum">     105 </span>            :   void TestAll();
<span class="lineNum">     106 </span>            :   void Test(const int test_net_id = 0);
<span class="lineNum">     107 </span>            :   virtual void SnapshotSolverState(const string&amp; model_filename) = 0;
<span class="lineNum">     108 </span>            :   virtual void RestoreSolverStateFromHDF5(const string&amp; state_file) = 0;
<span class="lineNum">     109 </span>            :   virtual void RestoreSolverStateFromBinaryProto(const string&amp; state_file) = 0;
<span class="lineNum">     110 </span>            :   void DisplayOutputBlobs(const int net_id);
<span class="lineNum">     111 </span>            :   void UpdateSmoothedLoss(Dtype loss, int start_iter, int average_loss);
<span class="lineNum">     112 </span>            : 
<span class="lineNum">     113 </span>            :   SolverParameter param_;
<span class="lineNum">     114 </span>            :   int iter_;
<span class="lineNum">     115 </span>            :   int current_step_;
<span class="lineNum">     116 </span>            :   shared_ptr&lt;Net&lt;Dtype&gt; &gt; net_;
<span class="lineNum">     117 </span>            :   vector&lt;shared_ptr&lt;Net&lt;Dtype&gt; &gt; &gt; test_nets_;
<span class="lineNum">     118 </span>            :   vector&lt;Callback*&gt; callbacks_;
<span class="lineNum">     119 </span>            :   vector&lt;Dtype&gt; losses_;
<span class="lineNum">     120 </span>            :   Dtype smoothed_loss_;
<span class="lineNum">     121 </span>            : 
<span class="lineNum">     122 </span>            :   // A function that can be set by a client of the Solver to provide indication
<span class="lineNum">     123 </span>            :   // that it wants a snapshot saved and/or to exit early.
<span class="lineNum">     124 </span>            :   ActionCallback action_request_function_;
<span class="lineNum">     125 </span>            : 
<span class="lineNum">     126 </span>            :   // True iff a request to stop early was received.
<span class="lineNum">     127 </span>            :   bool requested_early_exit_;
<span class="lineNum">     128 </span>            : 
<span class="lineNum">     129 </span>            :   // Timing information, handy to tune e.g. nbr of GPUs
<span class="lineNum">     130 </span>            :   Timer iteration_timer_;
<span class="lineNum">     131 </span>            :   float iterations_last_;
<span class="lineNum">     132 </span>            : 
<span class="lineNum">     133 </span>            :   DISABLE_COPY_AND_ASSIGN(Solver);
<span class="lineNum">     134 </span>            : };
<span class="lineNum">     135 </span>            : 
<span class="lineNum">     136 </span>            : }  // namespace caffe
<span class="lineNum">     137 </span>            : 
<span class="lineNum">     138 </span>            : #endif  // CAFFE_SOLVER_HPP_
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
